package com.sunchen.asc.service.item.server;

import com.sunchen.asc.service.common.ResultResponse;
import com.sunchen.asc.service.common.ResultUtil;
import com.sunchen.asc.service.item.api.ItemDepartmentService;
import com.sunchen.asc.service.item.domain.ItemDepartment;
import com.sunchen.asc.service.item.repository.ItemDepartmentRepository;
import com.sunchen.asc.service.item.repository.ItemRepository;
import com.sunchen.asc.service.item.vo.ItemDepartmentVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;

@DubboService(retries = 0)
public class ItemDepartmentServiceImpl implements ItemDepartmentService {

    @Autowired
    ItemDepartmentRepository itemDepartmentRepository;
    @Autowired
    ItemRepository itemRepository;

    public ResultResponse create(String name, String code,Long parentId, String description) {

        Long count = itemDepartmentRepository.countByNameOrCodeAndDeleteFlag(name,code,0);
        if (count > 0){
            return ResultUtil.failed("名称或编码已存在");
        }
        ItemDepartment itemDepartment = new ItemDepartment();
        itemDepartment.setName(name);
        itemDepartment.setCode(code);
        itemDepartment.setParentId(parentId);
        itemDepartment.setDeleteFlag(0);
        itemDepartment.setDescription(description);
        //acceptDepartment.setCreateUser();
        itemDepartmentRepository.save(itemDepartment);
        return ResultUtil.success("创建成功");
    }

    public ResultResponse edit(Long id, String name, String code, String description) {

        ItemDepartment itemDepartment = itemDepartmentRepository.getOne(id);
        if (!itemDepartment.getName().equals(name)){
            Long countN = itemDepartmentRepository.countByNameAndDeleteFlag(name,0);
            if (countN > 0){
                return ResultUtil.failed("名称已存在");
            }
        }
        if (!itemDepartment.getCode().equals(code)){
            Long countC = itemDepartmentRepository.countByCodeAndDeleteFlag(code,0);
            if (countC > 0){
                return ResultUtil.failed("编码已存在");
            }
        }
        itemDepartment.setName(name);
        itemDepartment.setCode(code);
        itemDepartment.setDescription(description);
        //acceptDepartment.setUpdateUser();
        itemDepartmentRepository.save(itemDepartment);
        return ResultUtil.success("修改成功");
    }

    public ResultResponse delete(Long id) {

        Long countI = itemRepository.countByAcceptDeptIdAndDeleteFlag(id,0);
        if (countI > 0){
            return ResultUtil.failed("存在绑定事项，无法删除");
        }
        Long countD = itemDepartmentRepository.countByParentIdAndDeleteFlag(id,0);
        if (countD > 0){
            return ResultUtil.failed("存在子部门，无法删除");
        }
        ItemDepartment itemDepartment = itemDepartmentRepository.getOne(id);
        itemDepartment.setDeleteFlag(1);
        itemDepartment.setDeleteTime(new Timestamp(System.currentTimeMillis()));
        //acceptDepartment.setDeleteUser();
        itemDepartmentRepository.save(itemDepartment);
        return ResultUtil.success("成功");
    }

    public ResultResponse all() {

        List<ItemDepartmentVo> vos = itemDepartmentRepository.findAllVo();
        return ResultUtil.success(vos);
    }

}
